import { User } from "../../hall/User"
import { Big } from "./Big"
import { GameMgr } from "../GameMgr"
import { Small } from "./Small"
import Logger from "../../utils/Logger"

export class Player{

    gameMgr:GameMgr
    user:User
    username:String
    connection
    small:Small
    arr_bigs = new Set<Big>()
    num_bornBig = 0                 //累计出生大子
    is_death:boolean = false        //是否玩家死亡

    constructor(gameMgr:GameMgr, user:User, pointId:String) {
        this.gameMgr = gameMgr
        this.user = user
        this.username = user.username
        this.connection = user.connection
        this.small = new Small(gameMgr, this, this.gameMgr.map_point.get(pointId))
        user.player = this
    }

    gc() {
        this.gameMgr = null
        this.user = null
        this.connection = null
        this.small?.gc()
        this.small = null
        this.arr_bigs?.forEach(big => {
            big.gc()
        })
        this.arr_bigs = null
    }

    /** 每个类都有，将需要的数据打包放进去，会传送到客户端 */
    toObj() {
        let obj = {
            "username":this.username,
            "small":this.small.toObj(),
        }
        return obj
    }

    /** 回合开始 */
    turnStart() {
        Logger.log(this.username, "回合开始")
        this.small.turnStart()
        this.arr_bigs.forEach(big => {
            big.turnStart()
        })
    }

    /** 回合结束 */
    turnOver() {
        Logger.log(this.username, "回合结束")
        this.small.turnOver()
        this.arr_bigs.forEach(big => {
            big.turnOver()
        })
    }
}